﻿@inherits PageLayout

<KLayout Layout="this">
    @if (IsLoaded)
    {
        <Spin Spinning="showSpin" Tip="@spinTip">
            @if (Context.IsMobile)
            {
                <AppLayout>@Body</AppLayout>
            }
            else
            {
                <Layout Style="min-height:100vh;">
                    <Sider Breakpoint="@BreakpointType.Lg" CollapsedWidth="60" Collapsible>
                        <div class="kui-logo"></div>
                        <div class="kui-scroll">
                            <AntMenu Accordion="Context.UserSetting.Accordion" Items="UserMenus" />
                        </div>
                    </Sider>
                    <Layout>
                        <Header Class="kui-header">
                            <TopBreadcrumb Current="Context.Current" />
                            <TopNavRight OnMenuClick="OnMenuClick" />
                        </Header>
                        <Content Class="kui-page">
                            @if (Context.UserSetting.MultiTab)
                            {
                                <ReuseTabs Class="top-row px-4" TabPaneClass="content px-4" />
                            }
                            else
                            {
                                <div class="kui-page-body">@Body</div>
                            }
                        </Content>
                        @if (AntConfig.Option.ShowFooter)
                        {
                            if (AntConfig.Option.Footer != null)
                            {
                                <Footer Style="text-align:center">@AntConfig.Option.Footer</Footer>
                            }
                            else
                            {
                                <Footer Style="text-align:center"><PageFooter /></Footer>
                            }
                        }
                    </Layout>
                </Layout>
            }
        </Spin>
        @if (IsLogin && !Context.IsMobile)
        {
            <Drawer Closable="true" @bind-Visible="showSetting" Placement="right" Title='(Language["Nav.Setting"])' Width="300">
                <SettingForm @ref="setting" />
            </Drawer>
        }
    }
</KLayout>

@code {
    private string spinTip = "";
    private bool showSpin = false;
    //Admin
    private bool showSetting = false;
    private SettingForm setting;

    public override async Task ShowSpinAsync(string text, Func<Task> action)
    {
        if (action == null)
            return;

        showSpin = true;
        spinTip = text;
        await StateChangedAsync();
        await Task.Run(async () =>
        {
            try
            {
                await action?.Invoke();
            }
            catch (Exception ex)
            {
                await OnError(ex);
            }
            showSpin = false;
            await StateChangedAsync();
        });
    }

    //Admin
    private void OnMenuClick(string id)
    {
        switch (id)
        {
            case "logout":
                UI?.Confirm(Language["Tip.Exits"], SignOutAsync);
                break;
            case "setting":
                showSetting = true;
                StateHasChanged();
                break;
        }
    }
}